<?php
class VcardAction extends MemberAction{
	public $db;
	public $pagezise=10;
	public $listmap=1;
	public $listmapnews=1;
	public $listorder='id';
	public function _initialize(){
		parent::_initialize();
		
		
		if(M('Member') -> where(array('id' => $this->memberInfo['id'])) -> getField('is_business') == 0){
			$this->error('您现在还不是商家');exit;
		}
		
		
		$this->db=M('Vcard');
		$this->info=D('Info');
		$this->listmap .= " and member_id=".$this->memberInfo['id']." and type='art'";
		$this->listmapnews .= " and member_id=".$this->memberInfo['id']." and type='news'";
		
		
		$vcard = $this->db->where(array('member_id'=>$this->userInfo['id']))->find();
		if(!$vcard){
			$this->db -> add(array('member_id'=>$this->userInfo['id']));
		}
		$vcard = $this->db->where(array('member_id'=>$this->userInfo['id']))->find();
		$this -> assign('vcard', $vcard);
	}
	
	//删除产品
	public function del(){
		
		$id=$_POST['proid'];
		$reluts=$this->info->where("id=$id")->delete(); 
		if($reluts >0 ){
			$data['a']="1";
			echo json_encode($data); 
			}
		
		 
		}
 
		
		
		//删除收藏
	public function dels(){
		$sc=M('Vcard_sc');
		 
		$where['zid']=$this->memberInfo['id'];
		$where['vid']=$_POST['scid'];
		$reluts=$sc->where($where)->delete($where); 
		if($reluts >0 ){
			$data['a']="1";
			echo json_encode($data);
			}
		
		 
		}
	
	//会员编辑信息
	public function editProduct(){
		
		
		
		import ( 'ORG.Util.Page' );
		$count=$this->info->where($this->listmap)->count();
		$p=new Page($count,$this->pagezise);
		$this->list = $this->info->where($this->listmap)->order($this->listorder)->limit($p->firstRow.',
	'.$p->listRows)->select();
	
		$this->listnews = $this->info->where($this->listmapnews)->order($this->listorder)->limit($p->firstRow.',
		'.$p->listRows)->select();
	
		$this->pages=$p->show();
		
		
	 
	    $sc=M('Vcard_sc');
		$where['zid']=$this->userInfo['id'];
		$scid=$sc->where($where)->getField('vid',true);
		 foreach($scid as $v){
			 $scs[]=$this->db->where("member_id='$v'")->find();
			 
			 }
	     $this->assign('scs',$scs);
		 
	 
	   	//微信 
	 	$this->assign('is_weixin',$this->is_weixin());
		$this->display('edit');
	}
 	
	//会员编辑信息
	public function edit(){
		if($this->is_weixin()){
		Vendor('Weixin.Weixin');
		$d['token']=getSysVal('token');
		$d['appid']=getSysVal('appid');
		$d['appsecret']=getSysVal('appsecret');
		$this->weixin=new WeixinChat($d);
		$this->assign('addressSign_info', $this->weixin->getSignPackage('#pagetwo15'));

		//dump($this->weixin->getSignPackage());
		}
		
		import ( 'ORG.Util.Page' );
		$count=$this->info->where($this->listmap)->count();
		$p=new Page($count,$this->pagezise);
		$this->list = $this->info->where($this->listmap)->order($this->listorder)->limit($p->firstRow.',
	'.$p->listRows)->select();
	
		$this->listnews = $this->info->where($this->listmapnews)->order($this->listorder)->limit($p->firstRow.',
		'.$p->listRows)->select();
		
		$this->pages=$p->show();
		
		$this->info = $this->db->where(array('member_id'=>$this->userInfo['id']))->find();
		
		
		
	    //大图
		$bg=$this->info;
		$bg=$bg['bg']; 
		$bg=$bg==''?array():explode(',',$bg);
		$this->assign('banner',$bg);
		
		//end大图
	    $sc=M('Vcard_sc');
		$where['zid']=$this->userInfo['id'];
		$scid=$sc->where($where)->getField('vid',true);
		 foreach($scid as $v){
			 $scs[]=$this->db->where("member_id='$v'")->find();
			 
			 }
	     $this->assign('scs',$scs);
		 
	 
	   	//微信 
	 	$this->assign('is_weixin',$this->is_weixin());
		$this->display();
	}
	//会员更新信息
	public function update(){  
		if(isset($_POST)){
			$info = $this->db->where(array('member_id'=>$this->userInfo['id']))->find();
			if(!is_array($info)){
				$_POST['member_id'] = $this->userInfo['id'];
				if(!$this->db->create()){
				    $this->error($this->db->getError());
			    }
				$re = $this->db->add();
			}else{
				if(!$this->db->create()){
				    $this->error($this->db->getError());
			    }
				$re = $this->db->where(array('member_id'=>$this->userInfo['id']))->save();
			}
			
			if(false !== $re){
				if($_POST['moban']){
					
					$Mbcount=M('Mbcount');
				 	if($_POST['moban'] == 1){ $Mbcount->where("id = 1")->setInc('mb1');}
					if($_POST['moban'] == 2){ $Mbcount->where("id = 1")->setInc('mb2');}
					if($_POST['moban'] == 3){ $Mbcount->where("id = 1")->setInc('mb3');}
					if($_POST['moban'] == 4){ $Mbcount->where("id = 1")->setInc('mb4');}
					
					echo "<script>alert('选择成功！');location.href='".__APP__."/Vcard/index/member_id/".$this->memberInfo['id']."';</script>";
					
					}
				elseif($this->_post('meiyou')=='1'){
					redirect(U('Public/getWeixinAddress'));
					  
					exit();
					}
				
				else{
					if($_POST['bg']){ redirect(__APP__."/Member/Vcard/edit#pagetwo18"); exit;}
					echo "<script>alert('编辑成功！');history.go(-1);</script>";}
				
				
				
				//$this->success('编辑成功');
			}else{
				$this->error($this->db->getError());
			}
		}else{
			$this->error('没有提交信息');
		}
	}
	public function uploadImg(){
		
 		$png= $this->_post('png');
		 
		if(!isset($png)){
			$this->display();exit();
		}else{
			
		function base64_to_png($inputfile, $outputfile ) { 
		$imageData = $inputfile; 
		$file = fopen( $outputfile, "w" ); 
		fwrite( $file, base64_decode( $imageData ) ); 
		fclose($file); 
		}
		
		$path=$_SERVER['DOCUMENT_ROOT'].'/Public/Uploads/card/'.$this->memberInfo['id'].'/'.time().'.jpg';
		
		
		if(!is_dir(pathinfo($path,PATHINFO_DIRNAME).'/')){
			mkdir(pathinfo($path,PATHINFO_DIRNAME).'/',777,true);
		}
		
		base64_to_png("".str_replace('data:image/jpeg;base64,','',$png), $path); 
		    $path2='/Public/Uploads/card/'.$this->memberInfo['id'].'/'.time().'.jpg';	
		    $url['img_url']=$path2;
  			$this->db->where(array('member_id'=>$this->userInfo['id']))->save($url);
		 
           //echo "<img src='".$path2."' />";
		   
			echo "<script>window.parent.uploaded('1','{$path2}');</script>";	
			$this->display();
		}
		/*	
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();						// 实例化上传类
		 		//设置上传图片的大小
		$upload->allowExts = array('jpg','png','gif','bmp');	//设置上传图片的后缀
		$upload->uploadReplace = true;					//同名则替换
		$upload->saveRule = 'uniqid';					//设置上传头像命名规则(临时图片),修改了UploadFile上传类
		//完整的头像路径
		$path = './Public/Uploads/card/'.$this->memberInfo['id'].'/';
		if(!is_dir($path)){
			mkdir($path, 777, true);
		}
		$upload->savePath = $path;
		if(!$upload->upload()) {						// 上传错误提示错误信息
		    exit("<script>window.parent.uploadImgerror();</script>");
			//$this->ajaxReturn('',$upload->getErrorMsg(),0,'json');
		}else{		$this->display();									// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			$temp_size = getimagesize($path.$info['0']['savename']);
			if($temp_size[0] < 100 || $temp_size[1] < 100){//判断宽和高是否符合头像要求
				$this->ajaxReturn(0,'图片宽或高不得小于100px！',0,'json');
				exit("<script>window.parent.uploaded('0','图片宽或高不得小于100px！');</script>");
			} 
			$data['picName'] = $info['0']['savename'];
			$data['status'] = 1;
			$data['url'] = __ROOT__.'/Public/Uploads/card/'.$this->memberInfo['id'].'/'.$data['picName'];
  			$data['info'] = $info;
 			
			
			
			
		    $dstW=300;//缩略图宽
     	    $dstH=300;//缩略图高

       		$src_image=ImageCreateFromJPEG($path);
    	    $srcW=ImageSX($src_image); //获得图片宽
   		    $srcH=ImageSY($src_image); //获得图片高
        
    	    $dst_image=ImageCreateTrueColor($dstW,$dstH);
 	        ImageCopyResized($dst_image,$src_image,0,0,0,0,$dstW,$dstH,$srcW,$srcH);
 	        ImageJpeg($dst_image,$path);
			$url['img_url']=$data['url'];
  			$this->db->where(array('member_id'=>$this->userInfo['id']))->save($url);
		 
            echo "<img src='".$data['url']."' />";
			exit("<script>window.parent.uploaded('1','{$data['url']}');</script>");
		
		*/
		 
	}
	
	//上传背景
	public function uploadbg(){
		if(!isset($_FILES['file'])){
			$this->display();exit();
		}else{
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();						// 实例化上传类
		//$upload->maxSize = 150*1024;					//设置上传图片的大小
		$upload->allowExts = array('jpg','png','gif');	//设置上传图片的后缀
		$upload->uploadReplace = true;					//同名则替换
		$upload->saveRule = 'uniqid';					//设置上传头像命名规则(临时图片),修改了UploadFile上传类
		//完整的头像路径
		$path = './Public/Uploads/bg/'.$this->memberInfo['id'].'/';
		if(!is_dir($path)){
			mkdir($path, 777, true);
		}
		$upload->savePath = $path;
		if(!$upload->upload()) {						// 上传错误提示错误信息
		    exit("<script>window.parent.uploadImgerrorbg();</script>");
			//$this->ajaxReturn('',$upload->getErrorMsg(),0,'json');
		}else{		$this->display();									// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			$temp_size = getimagesize($path.$info['0']['savename']);
			if($temp_size[0] < 100 || $temp_size[1] < 100){//判断宽和高是否符合头像要求
				$this->ajaxReturn(0,'图片宽或高不得小于100px！',0,'json');
				exit("<script>window.parent.uploadedbg('0','图片太大！');</script>");
			} 
			$data['picName'] = $info['0']['savename'];
			$data['status'] = 1;
			$data['url'] = __ROOT__.'/Public/Uploads/bg/'.$this->memberInfo['id'].'/'.$data['picName'];
			$data['info'] = $info;
			$url['bg']=$data['url'];
			$u=$data['url'];
			$this->db->where(array('member_id'=>$this->userInfo['id']))->save($url);
			
			exit("<script>window.parent.uploadedbg('1','{$u}');</script>");
		}}
	}
	
	//
	public function uploadVcf(){ 
		if(!isset($_FILES['vcf'])){
			$this->display();exit();
		}else{
		
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();						// 实例化上传类
 		$upload->allowExts = array('vcf');	//设置上传图片的后缀
		$upload->uploadReplace = true;					//同名则替换
		$upload->saveRule = 'vcf';					//设置上传头像命名规则(临时图片),修改了UploadFile上传类
		//完整的头像路径
		$path = './Public/Uploads/vcf/'.$this->memberInfo['id'].'/';
		 
		if(!is_dir($path)){
			mkdir($path, 777, true);
		}
		$upload->savePath = $path;
		if(!$upload->upload()) {						// 上传错误提示错误信息
			$this->ajaxReturn('',$upload->getErrorMsg(),0,'json');
		}else{		$this->display();									// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			$temp_size = getimagesize($path.$info['0']['savename']);
			 
			$data['picName'] = $info['0']['savename'];
			$data['status'] = 1;
			$data['url'] = __ROOT__.'/Public/Uploads/vcf/'.$this->memberInfo['id'].'/'.$data['picName'];
 			$data['info'] = $info;
			exit("<script>window.parent.uploaded1('1','{$data['url']}');</script>"); 
			
			
		}}
	}
	
	//判断是否为微信
	public function is_weixin(){ 
	if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
		 return true;
	}	
	    return false;
    }
	
	
	//剪切头像
	public function editx(){
 	if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {  
 	$id=$this->memberInfo['id'];
	$src=$this->db->where("member_id= $id")->getField('img_url');	
	$src='.'.$src; 
	$images=getimagesize($src);
 	$targ_w = $targ_h=300;
	 
	$jpeg_quality=100;
 	$img_r = imagecreatefromjpeg($src);
	$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
    imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
	$targ_w,$targ_h,$_POST['w'],$_POST['h']);
 	imagejpeg($dst_r,$src,$jpeg_quality);
	$url=U('Member/Vcard/edit');
 	echo "<script>alert('上传成功！');location.href='$url#pagetwo1'</script>";
	exit;
	 
  
    }
 
	
	}
	
	//剪切头像
	public function Jcrop(){
 	if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {  
 	$id=$this->memberInfo['id'];
	//$src=$this->db->where("member_id= $id")->getField('img_url');	
	
	$src='.'.$_POST['img_url'] = $_POST['src']; 
	$bigsrc = pathinfo($src, PATHINFO_DIRNAME).'/thumb_'.pathinfo($src, PATHINFO_FILENAME).'.'.pathinfo($src, PATHINFO_EXTENSION);
	
	$images=getimagesize($src);
	
 	//$targ_w = $targ_h=540;
	$targ_w=$this->_post('w');
	$targ_h=$this->_post('h');
	 
	$jpeg_quality=100;
 	$img_r = imagecreatefromjpeg($src);
	//保存原图
	
	imagejpeg($img_r,$bigsrc,$jpeg_quality);
	
	
	
	$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
    imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
	$targ_w,$targ_h,$_POST['w'],$_POST['h']);
 	imagejpeg($dst_r,$src,$jpeg_quality);
	//$url=U('Member/Vcard/edit');
 	echo 'OK';
	exit;
	 
  
    }
 
	
	}
	
	
	public function updateadd(){
		if($this->_post('coords')){
			$data['mapapi'] = $this->_post('coords');
			$data['maps'] = $this->_post('maps');
			$db= M('Vcard');
			 
			$ret = $db -> where(array('member_id' => $this->memberInfo['id'])) -> data($data) -> save();
			if($ret !== false){
				$this->success('更新地址成功');
			}else{
				$this->error($db -> getError());
			}
		}
	}
	
	public function ditu(){
		$db= M('Vcard');
		
		$info = $db->where(array('member_id'=>$this->memberInfo['id']))->find();
		$this->assign('info',$info);
		
		
		
		$this->display();
		
		}
	
	//会员写入信息
	public function insert(){
		$this->db=D('Info');
		$_POST['member_id'] = $this->memberInfo['id'];
		$_POST['site_id']=__SITEID__;
		if($this->db->create()){
			$re = $this->db->add();
			if($re){
				
				
			echo "<script>alert('添加成功！');history.go(-1);</script>";	
				 
				
              // $this->success('添加成功');
			}else{
				$this->error($this->db->getError()); 
			}
		}else{ 
			$this->error($this->db->getError());
		}
	}
	
	//会员更新信息
	public function updateinfo(){
		$this->db=D('Info');
		if($this->_post('id')){
			$id=$this->_post('id');
			if(!$this->db->create()){
				$this->error($this->db->getError());
			}
			if(false !== $this->db->where(array('id'=>$id))->save()){
				echo "<script>alert('添加成功！');history.go(-1);</script>";	
				//$this->success('编辑成功');
			}else{
				$this->error($this->db->getError());
			}
		}else{
			$this->error('没有提交信息');
		}
	}
	//公司介绍页
	
	public function company(){
		if($this->is_weixin()){
		Vendor('Weixin.Weixin');
		$d['token']=getSysVal('token');
		$d['appid']=getSysVal('appid');
		$d['appsecret']=getSysVal('appsecret');
		$this->weixin=new WeixinChat($d);
		$this->assign('addressSign_info', $this->weixin->getSignPackage('#pagetwo15'));

		//dump($this->weixin->getSignPackage());
		}
		
		import ( 'ORG.Util.Page' );
		$count=$this->info->where($this->listmap)->count();
		$p=new Page($count,$this->pagezise);
		$this->list = $this->info->where($this->listmap)->order($this->listorder)->limit($p->firstRow.',
	'.$p->listRows)->select();
	
		$this->listnews = $this->info->where($this->listmapnews)->order($this->listorder)->limit($p->firstRow.',
		'.$p->listRows)->select();
		
		$this->pages=$p->show();
		
		$this->info = $this->db->where(array('member_id'=>$this->userInfo['id']))->find();
	 
	    $sc=M('Vcard_sc');
		$where['zid']=$this->userInfo['id'];
		$scid=$sc->where($where)->getField('vid',true);
		 foreach($scid as $v){
			 $scs[]=$this->db->where("member_id='$v'")->find();
			 
			 }
	     $this->assign('scs',$scs);
		 
	 
	   	//微信 
	 	$this->assign('is_weixin',$this->is_weixin());
		//查询选择模板数
		$Mbcount=M('Mbcount');
		$mbs=$Mbcount->where("id = 1")->find();
		$this->assign('mbs',$mbs);
		
		if($this->_get('type')=='jc'){$this->display('company_jc');}
		if($this->_get('type')=='xx'){$this->display('company_xx');}
		if($this->_get('type')=='mo'){$this->display('moban');}
		
		 
		
		
		}
}